package umage.core;

import java.io.File;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.UIManager;

import org.jhotdraw.app.ApplicationModel;
import org.jhotdraw.app.DefaultMDIApplication;
import org.jhotdraw.app.action.NewAction;
import org.jhotdraw.app.action.OpenAction;

public class UMageApplication extends DefaultMDIApplication {
	
	// Disable default behavior
	// We don't want a random project being created at startup
	@Override
	public void start() {}
	
	// Reconfigure Actions to handle multiple project types
	@Override
	protected void initApplicationActions() {
		super.initApplicationActions();
		ApplicationModel mo = getModel();
		mo.putAction(NewAction.ID, new MultiNewAction(this));
		mo.putAction(OpenAction.ID, new MultiOpenAction(this));
	}
	
	// Apply a bug fix to look and feel to prevent odd text in some areas
	@Override
	protected void initLookAndFeel() {
		super.initLookAndFeel();
		// Bug fix code
		if (UIManager.getString("OptionPane.css") == null) {
            UIManager.put("OptionPane.css", "");
        }
	}
	
	// Modify updateOpenRecentMenu (a private method forced to protected) so that it can support multiple project types
	@Override
	protected void updateOpenRecentMenu(JMenu openRecentMenu) {
        if (openRecentMenu.getItemCount() > 0) {
            JMenuItem clearRecentFilesItem = (JMenuItem) openRecentMenu.getItem(
                    openRecentMenu.getItemCount() - 1
                    );
            openRecentMenu.removeAll();
            for (File f : recentFiles()) {
                openRecentMenu.add(new MultiOpenRecentAction(this, f));
            }
            if (recentFiles().size() > 0) {
                openRecentMenu.addSeparator();
            }
            openRecentMenu.add(clearRecentFilesItem);
        }
	}
	
}
